Have you run the code? There are several mistakes. First, resnet152.children() is a generator, and hence is not subscriptable. Also, resnet152 doesn’t take a requires_grad argument. The following code will work:
import torch
import torch.nn as nn
import torchvision.models as models
from torch.autograd import Variable
resnet152 = models.resnet152(pretrained=True)
modules=list(resnet152.children())[:-1]
resnet152=nn.Sequential(*modules)
for p in resnet152.parameters():
p.requires_grad = False
Of course, the outcome depends on what you want to achieve. This code returns a model consisting of all layers of resnet152 bar the last one (a fully connected layer), with fixed parameters
|